﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using LiveSequence.Common;
using Mono.Cecil;

namespace LiveSequence.Suite.Helpers
{
  public class AssemblyParserHelper
  {
    public static Dictionary<string, bool> GetAssemblyReferencesSet(string strFullPath)
    {
      Dictionary<string, bool> result = new Dictionary<string, bool>();

      AssemblyDefinition assemblyDef = AssemblyFactory.GetAssembly(strFullPath);
      foreach (AssemblyNameReference asmRef in assemblyDef.MainModule.AssemblyReferences)
      {
        // see if there is a file with asmRef.dll name
        // construct path
        // get the directory from the assemblyfilename
        string referencedAssemblyPath = Path.Combine(Path.GetDirectoryName(strFullPath),
                                                     asmRef.Name + ".dll");

        if (!result.Keys.Contains(asmRef.Name))
        {
          result.Add(asmRef.Name, true);
        }
        if (File.Exists(referencedAssemblyPath))
        {
          Dictionary<string, bool> setAssemblyReferences = GetAssemblyReferencesSet(referencedAssemblyPath);

          foreach(string strAssemblyRef in setAssemblyReferences.Keys)
          {
            if(!result.Keys.Contains(strAssemblyRef))
            {
              result.Add(strAssemblyRef, false);
            }
          }
          
        }
      }
      return result;
    }
  }
}
